import Vue from 'vue'
import { sessionStore } from 'store/dist/store.legacy'
const state = {
  url: process.env.VUE_APP_newsList_url,
  homeList: {},
  homeListPage: {},
  homeListLimit: 10,
  newsList: [],  //当前列表 list数据
  newsListLimit: 10
  position_1: [] //首页图最新图片推荐
  position_2: [] //首页最新内容推荐
}

const mutations = {
  //首页内容列表设置
  setHomeList (state, param) {
    Vue.set(state.homeList, param.catId, param.list)
    Vue.set(state.homeListPage, param.catId, param.page)
    sessionStore.set(`homeList_${param.catId}`, param.list)
    sessionStore.set('homeListPage', state.homeListPage)
  },
  //列表页内容初始设置
  newsListInit (state, catId) {
    let list = sessionStore.get(`newsList_${catId}`)
    if (!list) {
      Vue.set(state, 'newsList', [])
    } else {
      Vue.set(state, 'newsList', list)
    }
  },
  //列表页内容设置
  setNewsList (state, param) {
    state.newsList = state.newsList.concat(param.list)
    sessionStore.set(`newsList_${param.catId}`, state.newsList)
    let newsListPage = sessionStore.get('newsListPage') || {}
    newsListPage[param.catId] = param.page
    sessionStore.set('newsListPage', newsListPage)
  }
}

const actions = {
  //列表页内容更改
  async changeNewsList ({ commit, state }, catId) {
    let newsListPage = sessionStore.get('newsListPage'), page
    if (!newsListPage || !newsListPage[catId]) {
      page = 1
    } else {
      page = ++newsListPage[catId]
    }
    // console.log(page)
    return await window.axios.post(state.url, {
      cat_id: catId,
      page: page,
      limit: state.newsListLimit
    }).then(ret => {
      if (ret.data.length > 0) {
        commit('setNewsList', { catId: catId, page: page, list: ret.data })
        return 1
      } else {
        return 0
      }
    })
  },
  //首页内容列表初始化
  homeListInit ({ commit, state }, catId) {
    let list = state.homeList[catId]
    if (!list || list.length == 0) {
      list = sessionStore.get(`homeList_${catId}`)
      if (list) {
        commit('setHomeList', { catId: catId, page: 1, list: list })
      } else {
        window.axios.post(state.url, {
          cat_id: catId,
          page: 1,
          limit: 10
        }).then(ret => {
          commit('setHomeList', { catId: catId, page: 1, list: ret.data })
        })
      }
    }
  },
  //首页内容列表更改
  homeListChange ({ commit, state }, catId) {
    let homeListPage = sessionStore.get('homeListPage'), page
    if (!homeListPage || !homeListPage[catId]) {
      page = 1
    } else {
      page = ++homeListPage[catId]
    }
    window.axios.post(state.url, {
      cat_id: catId,
      page: page,
      limit: 10
    }).then(ret => {
      commit('setHomeList', { catId: catId, page: page, list: ret.data })
    })
  },
  positionChange ({ commit, state }, { param }) {
    let positionId = param.positionId
    let limit = param.limit ? param.limit : 0
    if (state['position_' + positionId].length > 0) {
      return
    }
    let positionList = sessionStore.get(`position_${positionId}`)
    if (!positionList) {
      let url = process.env.VUE_APP_getPosition_url
      window.axios.post(url, {
        position_id: positionId,
        limit
      }).then((ret) => {
        commit('setCatList', ret.data)
      })
    } else {
      commit('setCatList', catList)
    }
  }
}

export default {
  state,
  mutations,
  actions
}